#include "rs485_app.h"
#include "485_protocol.h"
#include "flash_api.h"
#include "sound_app.h"

// ProtocolFrameTable_t ProtocolTable[MAX_TABLE_SIZE] = INIT_TABLE;

static Rs485BitStatus_t Rs485BitStatus = {0};
extern void PromptIntoIoBoardUpdataFirmware(void);
bool ClearRs485BitStatus(void)
{
    memset(&Rs485BitStatus, 0, sizeof(Rs485BitStatus));
    return true;
}

bool Rs485BuildOpenSpecificFloorMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildOpenSpecificFloorMessage ->Rs485BitStatus.StepsOfSending = %d\r\n", Rs485BitStatus.StepsOfSending);
    bool MessageConstructionState = false;

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {

        switch (Rs485BitStatus.StepsOfSending)
        {
        case SEND_STEP_ONE:
        {
            MessageConstructionState = BuildOpenSpecificFoorFrameCallback(parmIn, pMessageOut, pSizeOut);
        }
        break;

        default:
            break;
        }
    }

    return MessageConstructionState;
}

uint8_t Rs485ParsingOpenSpecificFloorMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("Rs485ParsingOpenSpecificFloorMessage\r\n");
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    if (pDataIn != NULL)
    {
        ParsingState = ParsingOpenSpecificFoorFrameCallback((uint8_t *)pDataIn, pDataSizeIn);
        if (ParsingState == RS485_PARSING_SUCCESS)
        {
            ParsingResults = RS485_EXECUTE_OVER;
            Rs485BitStatus.StepsOfSending = RS485_ALL_COMMAND_EXECUTED;
        }
    }

    return ParsingResults;
}


bool Rs485BuildGetVersionMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildGetVersionMessage \r\n");
    bool MessageConstructionState = false;

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {

        switch (Rs485BitStatus.StepsOfSending)
        {
        case SEND_STEP_ONE:
        {
            MessageConstructionState = BuildGetVersionFrameCallback(parmIn, pMessageOut, pSizeOut);

        }
        break;

        default:
            break;
        }
    }

    return MessageConstructionState;
}

uint8_t Rs485ParsingGetVersionMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("Rs485ParsingGetVersionMessage\r\n");
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    if (pDataIn != NULL)
    {
        ParsingState = ParsingGetVersionFrameCallback((uint8_t *)pDataIn, pDataSizeIn);
        if (ParsingState == RS485_PARSING_SUCCESS)
        {
            ParsingResults = RS485_EXECUTE_OVER;
            Rs485BitStatus.StepsOfSending = RS485_ALL_COMMAND_EXECUTED;
        }
    }

    return ParsingResults;
}

bool Rs485BuildOpenEmergencyModeMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildOpenEmergencyModeMessage\r\n");
    bool MessageConstructionState = false;
    PRINT("[rs485_app] : Rs485BitStatus.StepsOfSending = %d\r\n", Rs485BitStatus.StepsOfSending);

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {

        PRINT("[rs485_app] : Rs485BuildOpenEmergencyModeMessage -> Rs485BitStatus.StepsOfSending = %d\r\n", Rs485BitStatus.StepsOfSending);
        MessageConstructionState = BuildOpenEmergencyModeFrameCallback(parmIn, pMessageOut, pSizeOut);
        PRINT("[rs485_app] : Rs485BuildOpenEmergencyModeMessage -> MessageConstructionState = %d\r\n", *pSizeOut);
    }

    return MessageConstructionState;
}

uint8_t Rs485ParsingOpenEmergencyModeMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("Rs485ParsingOpenEmergencyModeMessage\r\n");
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    if (pDataIn != NULL)
    {
        ParsingState = ParsingOpenEmergencyModeFrameCallback((uint8_t *)pDataIn, pDataSizeIn);
        PRINT("[rs485_app] : Rs485ParsingOpenEmergencyModeMessage -> ParsingState = %d\r\n", ParsingState);
        if (ParsingState == RS485_PARSING_SUCCESS)
        {
            PRINT("[rs485_app] : Rs485ParsingOpenEmergencyModeMessage -> ParsingState == RS485_PARSING_SUCCESS\r\n");
            ParsingResults = RS485_EXECUTE_OVER;
            Rs485BitStatus.StepsOfSending = RS485_ALL_COMMAND_EXECUTED;
        }
    }

    return ParsingResults;
}

extern uint32_t ProtocolGetFileSumSize(void);
extern void SetIoBoarGetOtaStatusNumberOfRetriesAndIngerval(void);
bool Rs485BuildIoBoardFirmwareTransMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildIoBoardFirmwareTransMessage Rs485BitStatus.StepsOfSending = %d\r\n", Rs485BitStatus.StepsOfSending);
    bool MessageConstructionState = false;

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {
        switch (Rs485BitStatus.StepsOfSending)
        {
        case SEND_STEP_ONE:
        {
            // 发送前的准备工作
            IoBoardFirmwareInfo_t *IoBoardFirmwareInfo = GetBoardFirmwareInfoPtr();
            if (IoBoardFirmwareInfo->CurrentSendPackageSeqNumber == 0) // 这是第一个包
            {
                PRINT("[protocol_api] : IoBoardFirmwareInfo->CurrentSendPackageSeqNumber == 0\r\n");
                // 设置固件总大小
                // IoBoardFirmwareInfo->IoBoardFirmwareSumSize = FlashGetIoBoardFirmwareMaxFileSize();
                uint32_t FirmwareSize = ProtocolGetFileSumSize();
                PRINT("[rs485_app]: FirmwareSize = %d\r\n", FirmwareSize);
                if (FirmwareSize == 0)
                {
                    IoBoardFirmwareInfo->IoBoardFirmwareSumSize = FlashGetIoBoardFirmwareMaxFileSize(); // 传输里面整个flash分区的大小
                }
                else
                {
                    IoBoardFirmwareInfo->IoBoardFirmwareSumSize = FirmwareSize; // 协议里面获取的大小
                }
                // 设置每个包发送的数据大小
                if (IoBoardFirmwareInfo->IoBoardFirmwareSumSize - IoBoardFirmwareInfo->SentDataSize < MESSAGE_DATA_PACKET_LENGTH_S)
                {
                    IoBoardFirmwareInfo->SinglePackageSendBufferSize = IoBoardFirmwareInfo->IoBoardFirmwareSumSize - IoBoardFirmwareInfo->SentDataSize;
                }
                else
                {
                    IoBoardFirmwareInfo->SinglePackageSendBufferSize = MESSAGE_DATA_PACKET_LENGTH_S;
                }
                // 计算出总发送的包数量
                if (IoBoardFirmwareInfo->IoBoardFirmwareSumSize % IoBoardFirmwareInfo->SinglePackageSendBufferSize)
                {
                    IoBoardFirmwareInfo->SumSentPackageNumber = (IoBoardFirmwareInfo->IoBoardFirmwareSumSize / IoBoardFirmwareInfo->SinglePackageSendBufferSize) + 1;
                }
                else
                {
                    IoBoardFirmwareInfo->SumSentPackageNumber = (IoBoardFirmwareInfo->IoBoardFirmwareSumSize / IoBoardFirmwareInfo->SinglePackageSendBufferSize);
                }
                // 计算出第一个包
                IoBoardFirmwareInfo->CurrentSendPackageSeqNumber = 0;
                // 设置当前发送的包的偏移
                IoBoardFirmwareInfo->SentDataSize = 0;
                // 从flash读取固件
                FlashReadIoBoardFirmware(IoBoardFirmwareInfo->SentDataSize, IoBoardFirmwareInfo->SinglePackageSendBuffer, IoBoardFirmwareInfo->SinglePackageSendBufferSize);
                // 打印数据
                PRINT("[protocol_api] : IoBoardFirmwareInfo->IoBoardFirmwareSumSize = %d\r\n", IoBoardFirmwareInfo->IoBoardFirmwareSumSize);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->SinglePackageSendBufferSize = %d\r\n", IoBoardFirmwareInfo->SinglePackageSendBufferSize);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->SumSentPackageNumber = %d\r\n", IoBoardFirmwareInfo->SumSentPackageNumber);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->CurrentSendPackageSeqNumber = %d\r\n", IoBoardFirmwareInfo->CurrentSendPackageSeqNumber);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->SentDataSize = %d\r\n", IoBoardFirmwareInfo->SentDataSize);
                // PRINT("[protocol_api] : IoBoardFirmwareInfo->SinglePackageSendBuffer[0] = %02x\r\n", IoBoardFirmwareInfo->SinglePackageSendBuffer[0]);
            }
            else
            {
                PRINT("[protocol_api] : IoBoardFirmwareInfo->CurrentSendPackageSeqNumber != 0\r\n");
                // 设置每个包发送的数据大小
                if (IoBoardFirmwareInfo->IoBoardFirmwareSumSize - IoBoardFirmwareInfo->SentDataSize < MESSAGE_DATA_PACKET_LENGTH_S)
                {
                    IoBoardFirmwareInfo->SinglePackageSendBufferSize = IoBoardFirmwareInfo->IoBoardFirmwareSumSize - IoBoardFirmwareInfo->SentDataSize;
                }
                else
                {
                    IoBoardFirmwareInfo->SinglePackageSendBufferSize = MESSAGE_DATA_PACKET_LENGTH_S;
                }
                // 读取固件的数
                FlashReadIoBoardFirmware(IoBoardFirmwareInfo->SentDataSize, IoBoardFirmwareInfo->SinglePackageSendBuffer, IoBoardFirmwareInfo->SinglePackageSendBufferSize);
                // 打印数据
                PRINT("[protocol_api] : IoBoardFirmwareInfo->IoBoardFirmwareSumSize = %d\r\n", IoBoardFirmwareInfo->IoBoardFirmwareSumSize);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->SinglePackageSendBufferSize = %d\r\n", IoBoardFirmwareInfo->SinglePackageSendBufferSize);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->SumSentPackageNumber = %d\r\n", IoBoardFirmwareInfo->SumSentPackageNumber);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->CurrentSendPackageSeqNumber = %d\r\n", IoBoardFirmwareInfo->CurrentSendPackageSeqNumber);
                PRINT("[protocol_api] : IoBoardFirmwareInfo->SentDataSize = %d\r\n", IoBoardFirmwareInfo->SentDataSize);
                // PRINT("[protocol_api] : IoBoardFirmwareInfo->SinglePackageSendBuffer[0] = %02x\r\n", IoBoardFirmwareInfo->SinglePackageSendBuffer[0]);
            }
            MessageConstructionState = BuildFirmwareTransFrameCallback(parmIn, pMessageOut, pSizeOut);
            IoBoardFirmwareInfo->SentDataSize += IoBoardFirmwareInfo->SinglePackageSendBufferSize;
        }
        break;

        case SEND_STEP_TWO: // 发送查询运行状态
        {
            PRINT("[protocol_api] : <------------Rs485BuildIoBoardFirmwareTransMessage SEND_STEP_TWO------------------------>\r\n");
            // todo: 设置发送间隔和发送数量
            SetIoBoarGetOtaStatusNumberOfRetriesAndIngerval();
            MessageConstructionState = BuildQueryIoBoardFirmwareStatusCallback(parmIn, pMessageOut, pSizeOut);
        }
        break;

        case SEND_STEP_THREE:
        {
            PRINT("[protocol_api] : Rs485BuildIoBoardFirmwareTransMessage SEND_STEP_THREE\r\n");
            MessageConstructionState = BuildGetVersionFrameCallback(parmIn, pMessageOut, pSizeOut);
        }
        break;

        case SEND_STEP_FOUR:
        {
            PRINT("[protocol_api] : BuildQueryIoBoardMacAddressCallback SEND_STEP_THREE\r\n");
            MessageConstructionState = BuildQueryIoBoardMacAddressCallback(parmIn, pMessageOut, pSizeOut);
        }
        break;

        default:
            break;
        }
    }

    return MessageConstructionState;
}


uint8_t Rs485ParsingIoBoardFirmwareTransMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("[rs485_app] :Rs485ParsingIoBoardFirmwareTransMessage Rs485BitStatus.StepsOfSending = %d\r\n", Rs485BitStatus.StepsOfSending);
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    if (pDataIn != NULL)
    {
        switch (Rs485BitStatus.StepsOfSending)
        {
        case SEND_STEP_ONE:
        {
            PRINT("[protocol_api] : Rs485ParsingIoBoardFirmwareTransMessage SEND_STEP_ONE\r\n");
            ParsingState = ParsingFirmwareTransFrameCallback((uint8_t *)pDataIn, pDataSizeIn);
            if (ParsingState == RS485_PARSING_SUCCESS)
            {

                // 所有的包都接收完成了IoBoardFirmwareInfo_t *IoBoardFirmwareInfo = (IoBoardFirmwareInfo_t *)dataIn;
                IoBoardFirmwareInfo_t *IoBoardFirmwareInfo = GetBoardFirmwareInfoPtr();
                IoBoardFirmwareInfo->CurrentSendPackageSeqNumber++;
                PRINT("[protocol_api] : IoBoardFirmwareInfo->CurrentSendPackageSeqNumber = %d\r\n", IoBoardFirmwareInfo->CurrentSendPackageSeqNumber);
                if (IoBoardFirmwareInfo->CurrentSendPackageSeqNumber > IoBoardFirmwareInfo->SumSentPackageNumber - 1)
                {
                    /** 接收固件完成 **/
                    Rs485BitStatus.StepsOfSending = SEND_STEP_TWO;
                }
                else
                {
                    /** 接收固件未完成 **/
                    Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                }
                ParsingResults = RS485_EXECUTE_SUCCESS;
            }
        }
        break;

        case SEND_STEP_TWO:
        {
            PRINT("[protocol_api] : Rs485ParsingIoBoardFirmwareTransMessage SEND_STEP_TWO\r\n");
            ParsingState = ParsingQueryIoBoardFirmwareStatusCallback((uint8_t *)pDataIn, pDataSizeIn);
            if (ParsingState == RS485_PARSING_SUCCESS)
            {
                uint8_t *RunningStatus = (uint8_t *)pDataIn;
                if (RunningStatus[0] == R_OTA_RUNNING)
                {
                    PRINT("[protocol_api] : R_OTA_RECEIVE_SUCCESS\r\n");
                    Rs485BitStatus.StepsOfSending = SEND_STEP_TWO;
                    ParsingResults = RS485_EXECUTE_SUCCESS;
                }
                else if (RunningStatus[0] == R_RUNNING)
                {
                    Rs485BitStatus.StepsOfSending = SEND_STEP_THREE;
                    ParsingResults = RS485_EXECUTE_SUCCESS;
                }
                else
                {
                    PRINT("[protocol_api] : R_OTA_RECEIVE_FAILURE ParsingState = %d\r\n", ParsingState);
                    ParsingResults = RS485_EXECUTE_OVER;
                }
            }
        }
        break;

        case SEND_STEP_THREE:
        {
            PRINT("[protocol_api] : ParsingGetVersionFrameCallback SEND_STEP_THREE\r\n");
            ParsingState = ParsingGetVersionFrameCallback((uint8_t *)pDataIn, pDataSizeIn);
            if (ParsingState == RS485_PARSING_SUCCESS)
            {
                // 结束
                SetIoBoardFirmwareExistFlag(false);
                PromptIntoIoBoardUpdataFirmware();
                PlaySettingStatusAudio(true);
                PRINT("[protocol_api] : firmware rec ok\r\n");
                ParsingResults = RS485_EXECUTE_SUCCESS;
                Rs485BitStatus.StepsOfSending = SEND_STEP_FOUR;
            }
        }
        break;

        case SEND_STEP_FOUR:
        {
            PRINT("[protocol_api] : Rs485ParsingIoBoardFirmwareTransMessage SEND_STEP_FOUR\r\n");
            ParsingState = ParsingQueryIoBoardMacAddressCallback((uint8_t *)pDataIn, pDataSizeIn);
            if (ParsingState == RS485_PARSING_SUCCESS)
            {
                PRINT("[protocol_api] : ParsingQueryIoBoardMacAddressCallback RS485_PARSING_SUCCESS\r\n");
                ParsingResults = RS485_EXECUTE_OVER;
                Rs485BitStatus.StepsOfSending = RS485_ALL_COMMAND_EXECUTED;
            }
        }
        break;

        default:
            break;
        }
    }

    return ParsingResults;
}


bool Rs485BuildQueryIoBoardFirmwareStatusMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildQueryIoBoardFirmwareStatusMessage\r\n");
    bool MessageConstructionState = false;

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {
        switch (Rs485BitStatus.StepsOfSending)
        {
        case SEND_STEP_ONE:
        {
            MessageConstructionState = BuildQueryIoBoardFirmwareStatusCallback(parmIn, pMessageOut, pSizeOut);
        }
        break;

        default:
            break;
        }
    }

    return MessageConstructionState;
}

uint8_t Rs485ParsingQueryIoBoardFirmwareStatusMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("Rs485ParsingQueryIoBoardFirmwareStatusMessage\r\n");
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    if (pDataIn != NULL)
    {
        ParsingState = ParsingQueryIoBoardFirmwareStatusCallback((uint8_t *)pDataIn, pDataSizeIn);
        if (ParsingState == RS485_PARSING_SUCCESS)
        {
            ParsingResults = RS485_EXECUTE_OVER;
            Rs485BitStatus.StepsOfSending = RS485_ALL_COMMAND_EXECUTED;
        }
    }

    return ParsingResults;
}

static uint8_t sendTimes = 0;
// extern IoboardDeviceInfo_t IoboardDeviceInfoTable[IOBOARD_DEVICE_NUM];
bool Rs485BuildQueryIoBoardMacAddressMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildQueryIoBoardMacAddressMessage Rs485BitStatus.StepsOfSending = %d\r\n", Rs485BitStatus.StepsOfSending);
    bool MessageConstructionState = false;

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {
        sendTimes++;
        PRINT("[rs485_app] : sendTimes = %d\r\n", sendTimes);
        switch (Rs485BitStatus.StepsOfSending)
        {
        case SEND_STEP_ONE:
        {
            uint8_t IoBoardAddress[MAC_ADDR_CODING_LENGTH] = {0};
            GetDestinMacAddress(IoBoardAddress);
            PRINT("[rs485_app] : Rs485BuildQueryIoBoardMacAddressMessage IoBoardAddress[5] = %d\r\n", IoBoardAddress[5]);
            switch (IoBoardAddress[5])
            {
            case NO_ONE_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_ONE_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    IoBoardAddress[5] = NO_ONE_ADDRESS_IO_BOARD;//问题所在
                }
            }
            break;

            case NO_TWO_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_TWO_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    IoBoardAddress[5] = NO_THREE_ADDRESS_IO_BOARD;
                }
            }
            break;

            case NO_THREE_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_THREE_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    IoBoardAddress[5] = NO_FOUR_ADDRESS_IO_BOARD;
                }
            }
            break;

            case NO_FOUR_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_FOUR_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    IoBoardAddress[5] = NO_FIVE_ADDRESS_IO_BOARD;
                }
            }
            break;

            case NO_FIVE_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_FIVE_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    IoBoardAddress[5] = NO_SIX_ADDRESS_IO_BOARD;
                }
            }
            break;

                {
                case NO_SIX_ADDRESS_IO_BOARD:
                    PRINT("[protocol_api]: NO_SIX_ADDRESS_IO_BOARD\r\n");
                    if (sendTimes > 2)
                    {
                        sendTimes = 0;
                        IoBoardAddress[5] = NO_SEVEN_ADDRESS_IO_BOARD;
                    }
                }
                break;

            case NO_SEVEN_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_SEVEN_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    IoBoardAddress[5] = NO_EIGHT_ADDRESS_IO_BOARD;
                }
            }
            break;

            case NO_EIGHT_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_EIGHT_ADDRESS_IO_BOARD\r\n");
                if (sendTimes > 2)
                {
                    sendTimes = 0;
                    // IoBoardAddress[5] = NO_ONE_ADDRESS_IO_BOARD;
                }
            }
            break;

            default:
            {
                IoBoardAddress[5] = NO_ONE_ADDRESS_IO_BOARD;
            }
            break;
            }
            SetDestinMacAddress(IoBoardAddress);
            MessageConstructionState = BuildQueryIoBoardMacAddressCallback(parmIn, pMessageOut, pSizeOut);
        }
        break;

        default:
            break;
        }
    }
    return false;
}

uint8_t Rs485ParsingQueryIoBoardMacAddressMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("Rs485ParsingQueryIoBoardMacAddressMessage\r\n");
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    IoboardDeviceInfo_t *IoboardDeviceInfoTable = GetIoBoardDeviceInfoTablePtr();
    if (pDataIn != NULL)
    {
        ParsingState = ParsingQueryIoBoardMacAddressCallback((uint8_t *)pDataIn, pDataSizeIn);
        if (ParsingState == RS485_PARSING_SUCCESS)
        {
            uint8_t IoBoardAddress[MAC_ADDR_CODING_LENGTH] = {0};
            GetDestinMacAddress(IoBoardAddress);
            PRINT("[re485_app.c] : Rs485ParsingQueryIoBoardMacAddressMessage IoBoardAddress[5] = %d\r\n", IoBoardAddress[5]);

            switch (IoBoardAddress[5])
            {
            case NO_ONE_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_ONE_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = 1; // 结束的标志
                SetDestinMacAddress(IoBoardAddress);
                // Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                ParsingResults = RS485_EXECUTE_OVER;
                sendTimes = 0;
                IoboardDeviceInfoTable[0].BoardIsExist = true;
            }
            break;

            case NO_TWO_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_TWO_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_THREE_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                sendTimes = 0;
                IoboardDeviceInfoTable[1].BoardIsExist = true;
            }
            break;

            case NO_THREE_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_THREE_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_FOUR_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                sendTimes = 0;
                IoboardDeviceInfoTable[2].BoardIsExist = true;
            }
            break;

            case NO_FOUR_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_FOUR_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_FIVE_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                sendTimes = 0;
                IoboardDeviceInfoTable[3].BoardIsExist = true;
            }
            break;

            case NO_FIVE_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_FIVE_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_SIX_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                sendTimes = 0;
                IoboardDeviceInfoTable[4].BoardIsExist = true;
            }
            break;

            case NO_SIX_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_SIX_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_SEVEN_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                sendTimes = 0;
                IoboardDeviceInfoTable[5].BoardIsExist = true;
            }
            break;

            case NO_SEVEN_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_SEVEN_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_EIGHT_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                sendTimes = 0;
                IoboardDeviceInfoTable[6].BoardIsExist = true;
            }
            break;

            case NO_EIGHT_ADDRESS_IO_BOARD:
            {
                PRINT("[protocol_api]: NO_EIGHT_ADDRESS_IO_BOARD\r\n");
                IoBoardAddress[5] = NO_ONE_ADDRESS_IO_BOARD;
                SetDestinMacAddress(IoBoardAddress);
                sendTimes = 0;
                ParsingResults = RS485_EXECUTE_OVER;
                IoboardDeviceInfoTable[7].BoardIsExist = true;
            }
            break;

            default:
            {
                // PRINT("[protocol_api]: NO_ONE_ADDRESS_IO_BOARD\r\n");
                // IoBoardAddress[5] = NO_TWO_ADDRESS_IO_BOARD;
                // SetDestinMacAddress(IoBoardAddress);
                // Rs485BitStatus.StepsOfSending = SEND_STEP_ONE;
                // sendTimes = 0;
            }
            break;
            }
        }
    }
    return ParsingResults;
}


bool Rs485BuildSetBoardSelfTestFrameMessage(void *parmIn, uint8_t *pMessageOut, uint16_t *pSizeOut, uint32_t *pResponseTimeOut)
{
    PRINT("[rs485_app] : Rs485BuildSetBoardSelfTestFrameMessage\r\n");
    bool MessageConstructionState = false;

    if ((pMessageOut != NULL) && (pSizeOut != NULL) && (pResponseTimeOut != NULL))
    {
        // switch (Rs485BitStatus.StepsOfSending)
        // {
        // case SEND_STEP_ONE:
        // {
            MessageConstructionState = BuildSetBoardSelfTestFrameCallback(parmIn, pMessageOut, pSizeOut);
        // }
        // break;

        // default:
        //     break;
    //  }
    }

    return MessageConstructionState;
}

uint8_t Rs485ParsingSetBoardSelfTestFrameMessage(const void *pDataIn, uint16_t pDataSizeIn)
{
    uint8_t ParsingResults = RS485_EXECUTE_ERROR;
    PRINT("Rs485ParsingSetBoardSelfTestFrameMessage\r\n");
    RS485MessageParsingResult_t ParsingState = RS485_PARSING_ERROR;

    if (pDataIn != NULL)
    {
        ParsingState = ParsingSetBoardSelfTestFrameCallback((uint8_t *)pDataIn, pDataSizeIn);
        if (ParsingState == RS485_PARSING_SUCCESS)
        {
            ParsingResults = RS485_EXECUTE_OVER;
            Rs485BitStatus.StepsOfSending = RS485_ALL_COMMAND_EXECUTED;
        }
    }

    return ParsingResults;
}
